package com.hjimi.ski.presenter;

import com.hjimi.ski.bean.DeviceImageBean;
import com.hjimi.ski.callback.BaseCallback;
import com.hjimi.ski.config.UrlConfig;
import com.hjimi.ski.iView.DeviceImageIView;
import com.hjimi.ski.model.base.DataModel;
import com.hjimi.ski.model.base.ModelToken;
import com.hjimi.ski.utils.JsonParseUtil;
import com.hjimi.ski.utils.ToastUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2023/2/11
 */
public class DeviceImagePresenter extends BasePresenter<DeviceImageIView>{
    @SuppressWarnings("unchecked")
    public void
    getDeviceImage(int id){
        String url = UrlConfig.deviceImage + id;
        DataModel.request(ModelToken.GET_REQUEST)
                .url(url)
                .execute(new BaseCallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        try {
                            JSONObject obj = new JSONObject(data);
                            if(obj.getInt("code")==200){
                                List<DeviceImageBean.DataBean> list = new ArrayList<>();
                                JSONArray array = new JSONArray(obj.getString("data"));
                                for(int i=0;i<array.length();i++){
                                    JSONObject object = new JSONObject(array.get(i).toString());
                                    DeviceImageBean.DataBean dataBean = new DeviceImageBean.DataBean(
                                            JsonParseUtil.getString(object,"name"),
                                            JsonParseUtil.getString(object,"imageUrl"),
                                            JsonParseUtil.getString(object,"thumbUrl"),
                                            JsonParseUtil.getString(object,"qrUrl"),
                                            JsonParseUtil.getInt(object,"artwork_id")
                                    );
                                    list.add(dataBean);
                                }
                                getView().showDeviceImage(list);

                            }else if(obj.getInt("code")==202){
                                getView().showDeviceImage(null);
                            }else if(obj.getInt("code")==204){
                                getView().showUnBind();
                            }else{
                                ToastUtils.show(obj.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        getView().showDeviceImage(null);
                        getView().showErr();
                    }

                    @Override
                    public void onComplete() {

                    }

                    @Override
                    public void onBefore() {

                    }
                });
    }
}
